package mi.payment.transaction;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import mi.payment.R;

/**
 * @author Created by longx on 2017/7/13.
 */
public class MenuItemView extends RelativeLayout {

    private TextView mTvMenu;
    private ImageView mIvIcon;
    private ImageView mIvArrow;

    public MenuItemView(Context context) {
        super(context);
        initView();
    }

    public MenuItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View.inflate(getContext(), R.layout.main_item_menu_lv, this);
        mIvIcon = findViewById(R.id.iv_icon);
        mTvMenu = findViewById(R.id.tv_menu);
        mIvArrow = findViewById(R.id.iv_arrow);
    }

    public void setData(int iconResId, int strResId) {
        String string = getContext().getString(strResId);
        setData(iconResId, string);
    }

    public void setData(int iconId, String str) {
        Spanned fromHtml = Html.fromHtml(str);
        int color = getResources().getColor(R.color.white);
        mIvIcon.setBackgroundResource(iconId);
        mTvMenu.setText(fromHtml);
        mIvArrow.setBackgroundResource(R.drawable.ic_arrow_white);
        mTvMenu.setTextColor(color);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setBackgroundResource(R.color.C0Cffffff);
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP:
                setBackgroundResource(R.color.C161718);
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }


}
